define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/auditlog/index' + location.search,
                    add_url: 'user/auditlog/add',
                    edit_url: 'user/auditlog/edit',

                    multi_url: 'user/auditlog/multi',
                    import_url: 'user/auditlog/import',
                    table: 'user_auditlog',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.id', title: __('User_id')},
                        {field: 'role_text', title: __('Role'), searchList: {"agent":__('role Agent'),"companion":__('role Companion'),"room":__('role Room')}, formatter: Table.api.formatter.normal},
                        {field: 'applicant', title:'申请人',operate: false,},
                        {field: 'img', title: __('Img'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'audit_status', title: __('Audit_status'), searchList: {"待审核":__('待审核'),"已审核":__('已审核'),"已驳回":__('已驳回')}, formatter: Table.api.formatter.status},
                        {field: 'age', title: __('Age'), operate: false},
                        {field: 'edu', title: __('Edu'), operate: false},
                        {field: 'exp_years', title: __('Exp_years'), operate: false},
                        {field: 'addr', title: __('Addr'), operate: false,table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'industry', title: __('Industry'), operate: false},
                        {field: 'audit_time', title: __('Audit_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'admin.nickname', title: __('Auditor'), operate: 'LIKE'},
                        {field: 'expected_salary', title: __('Expected_salary'), operate: false, table: table, class: 'autocontent'},
                        {field: 'certificate', title: __('Certificate'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'profile_image', title: __('Profile_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: function (value, row, index) {
                                if (row.audit_status !== '已审核') {
                                    // 如果状态不是已审核，显示编辑按钮
                                    return '<a href="' + $.fn.bootstrapTable.defaults.extend.edit_url + '/id/' + row.id + '" class="btn btn-xs btn-warning btn-editone" title="' + __('Edit') + '"><i class="fa fa-pencil"></i></a>';
                                }
                                // 返回空字符串，表示不显示编辑按钮
                                return '';
                            }
                        }


                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).ready(function () {
                const attachmentInput = $('#c-attachment');
                const downloadButton = $('#downloadAttachment');

                // 监听附件栏的变化
                attachmentInput.on('input', function () {
                    const attachmentValue = attachmentInput.val();

                    // 如果有附件，则显示下载按钮
                    if (attachmentValue) {
                        downloadButton.show();
                    } else {
                        downloadButton.hide();
                    }
                });

                // 下载按钮点击事件
                downloadButton.on('click', function () {
                    const attachmentUrls = attachmentInput.val().split(',').map(url => url.trim()); // 将附件链接分割并去除空格
                    let validUrlFound = false; // 用于检查是否有有效的 URL

                    // 创建一个链接元素并逐个下载
                    attachmentUrls.forEach(url => {
                        if (url) { // 检查是否为有效的 URL
                            validUrlFound = true;
                            const link = document.createElement('a');
                            link.href = url; // 设置下载链接
                            link.target = '_blank'; // 在新标签页中打开
                            link.download = ''; // 触发下载

                            // 触发下载
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        }
                    });

                    if (!validUrlFound) {
                        alert('没有有效的附件链接！');
                    }
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
